﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Infrastructure;

namespace Enterprise.UI.Web.Modules.WebEmail
{
    public partial class AddNew : Enterprise.Service.BasePage
    {
        protected string filePath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                Enterprise.Service.Email.EmailService es = new Service.Email.EmailService();
                string email = es.GetEmailAddressByUserId(Enterprise.Infrastructure.Utility.Get_UserId);
                if (es.IsOk(email))
                {
                    tb_from.Text = email;
                    filePath = System.Web.Configuration.WebConfigurationManager.AppSettings["EmailAttachmentsPath_Outbox"].ToString() + email;
                    filePath = FileHelper.Encrypt(filePath);
                }
                else
                {
                    Enterprise.Infrastructure.Utility.ShowMsg(this.Page, "Error", "邮件系统服务还未生效", "Setting.aspx");                    
                }
                
            }
        }

        protected void btn_Send_Click(object sender, EventArgs e)
        {
            string subject = tb_subject.Text;
            string attch = attch_list_value.Value;
            string[] files = attch.Split('|');
            string attchFilesToDataBase = "";//要存入数据的附件形式 d:\outbox\ssectest@163.com\
            foreach (string s in files)
            {
                if (!string.IsNullOrEmpty(s))
                {
                    attchFilesToDataBase += System.Web.Configuration.WebConfigurationManager.AppSettings["EmailAttachmentsPath_Outbox"].ToString() + tb_from.Text + "\\" + s + "|";
                }
            }
            if(attchFilesToDataBase.Length>0)
                attchFilesToDataBase = attchFilesToDataBase.Substring(0, attchFilesToDataBase.Length - 1);
            string content = (string)Enterprise.Infrastructure.Utility.sink(tb_content.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            if (string.IsNullOrEmpty(content))
                content = "<无内容>";
            if (string.IsNullOrEmpty(tb_subject.Text))
                subject = "<无主题>";
            int success=(int)Enterprise.Service.Email.MailState.NotSend;
            Enterprise.Service.Email.EmailService es = new Service.Email.EmailService();
            es.SendMail(tb_from.Text, tb_to.Text, tb_subject.Text, content, attchFilesToDataBase, success,tb_cc.Text);
            Enterprise.Infrastructure.Utility.ShowMsg(this.Page, "OK", this.Tran("操作成功"), "OutBox.aspx");
        }
    }
}